home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / TEMP / GNU / flex / Patterns < prev    next >
Text File  |  1995-06-28  |  8KB  |  304 lines

  1. Patterns
  2. Previous: <Format=>Format> * Next: <Matching=>Matching> * Up: <Top=>!Root>
  3.  
  4. #Wrap on
  5. {fH3}Patterns{f}
  6.  
  7. The patterns in the input are written using an extended
  8. set of regular expressions.  These are:
  9.  
  10. #Indent +4
  11. #Indent
  12. {fEmphasis}x{f}
  13. #Indent +4
  14. match the character {fEmphasis}x{f}
  15. #Indent
  16. {fEmphasis}.{f}
  17. #Indent +4
  18. any character (byte) except newline
  19. #Indent
  20. {fEmphasis}[xyz]{f}
  21. #Indent +4
  22. a "character class"; in this case, the pattern
  23. matches either an {fEmphasis}x{f}, a {fEmphasis}y{f}, or a {fEmphasis}z{f}
  24. #Indent
  25. {fEmphasis}[abj-oZ]{f}
  26. #Indent +4
  27. a "character class" with a range in it; matches
  28. an {fEmphasis}a{f}, a {fEmphasis}b{f}, any letter from {fEmphasis}j{f} through {fEmphasis}o{f},
  29. or a {fEmphasis}Z{f}
  30. #Indent
  31. {fEmphasis}[^A-Z]{f}
  32. #Indent +4
  33. a "negated character class", i.e., any character
  34. but those in the class.  In this case, any
  35. character EXCEPT an uppercase letter.
  36. #Indent
  37. {fEmphasis}[^A-Z\\n]{f}
  38. #Indent +4
  39. any character EXCEPT an uppercase letter or
  40. a newline
  41. #Indent
  42. {fEmphasis}{fStrong}r{f}\*{f}
  43. #Indent +4
  44. zero or more {fStrong}r{f}'s, where {fStrong}r{f} is any regular expression
  45. #Indent
  46. {fEmphasis}{fStrong}r{f}+{f}
  47. #Indent +4
  48. one or more {fStrong}r{f}'s
  49. #Indent
  50. {fEmphasis}{fStrong}r{f}?{f}
  51. #Indent +4
  52. zero or one {fStrong}r{f}'s (that is, "an optional {fStrong}r{f}")
  53. #Indent
  54. {fEmphasis}{fStrong}r{f}\{2,5\}{f}
  55. #Indent +4
  56. anywhere from two to five {fStrong}r{f}'s
  57. #Indent
  58. {fEmphasis}{fStrong}r{f}\{2,\}{f}
  59. #Indent +4
  60. two or more {fStrong}r{f}'s
  61. #Indent
  62. {fEmphasis}{fStrong}r{f}\{4\}{f}
  63. #Indent +4
  64. exactly 4 {fStrong}r{f}'s
  65. #Indent
  66. {fEmphasis}\{{fStrong}name{f}\}{f}
  67. #Indent +4
  68. the expansion of the "{fStrong}name{f}" definition
  69. (see above)
  70. #Indent
  71. {fEmphasis}"[xyz]\\"foo"{f}
  72. #Indent +4
  73. the literal string: {fEmphasis}[xyz]"foo{f}
  74. #Indent
  75. {fEmphasis}\\{fStrong}x{f}{f}
  76. #Indent +4
  77. if {fStrong}x{f} is an {fEmphasis}a{f}, {fEmphasis}b{f}, {fEmphasis}f{f}, {fEmphasis}n{f}, {fEmphasis}r{f}, {fEmphasis}t{f}, or {fEmphasis}v{f},
  78. then the ANSI-C interpretation of \\{fStrong}x{f}.
  79. Otherwise, a literal {fEmphasis}{fStrong}x{f}{f} (used to escape
  80. operators such as {fEmphasis}\*{f})
  81. #Indent
  82. {fEmphasis}\\0{f}
  83. #Indent +4
  84. a NUL character (ASCII code 0)
  85. #Indent
  86. {fEmphasis}\\123{f}
  87. #Indent +4
  88. the character with octal value 123
  89. #Indent
  90. {fEmphasis}\\x2a{f}
  91. #Indent +4
  92. the character with hexadecimal value {fCode}2a{f}
  93. #Indent
  94. {fEmphasis}({fStrong}r{f}){f}
  95. #Indent +4
  96. match an {fStrong}r{f}; parentheses are used to override
  97. precedence (see below)
  98. #Indent
  99. {fEmphasis}{fStrong}r{f}{fStrong}s{f}{f}
  100. #Indent +4
  101. the regular expression {fStrong}r{f} followed by the
  102. regular expression {fStrong}s{f}; called "concatenation"
  103. #Indent
  104. {fEmphasis}{fStrong}r{f}|{fStrong}s{f}{f}
  105. #Indent +4
  106. either an {fStrong}r{f} or an {fStrong}s{f}
  107. #Indent
  108. {fEmphasis}{fStrong}r{f}\/{fStrong}s{f}{f}
  109. #Indent +4
  110. an {fStrong}r{f} but only if it is followed by an {fStrong}s{f}.  The text
  111. matched by {fStrong}s{f} is included when determining whether this rule is
  112. the {fUnderline}longest match{f}, but is then returned to the input before
  113. the action is executed.  So the action only sees the text matched
  114. by {fStrong}r{f}.  This type of pattern is called {fUnderline}trailing context{f}.
  115. (There are some combinations of {fEmphasis}{fStrong}r{f}\/{fStrong}s{f}{f} that {fCode}flex{f}
  116. cannot match correctly; see notes in the Deficiencies \/ Bugs section
  117. below regarding "dangerous trailing context".)
  118. #Indent
  119. {fEmphasis}^{fStrong}r{f}{f}
  120. #Indent +4
  121. an {fStrong}r{f}, but only at the beginning of a line (i.e.,
  122. which just starting to scan, or right after a
  123. newline has been scanned).
  124. #Indent
  125. {fEmphasis}{fStrong}r{f}${f}
  126. #Indent +4
  127. an {fStrong}r{f}, but only at the end of a line (i.e., just
  128. before a newline).  Equivalent to "{fStrong}r{f}\/\\n".
  129.  
  130. Note that flex's notion of "newline" is exactly
  131. whatever the C compiler used to compile flex
  132. interprets '\\n' as; in particular, on some DOS
  133. systems you must either filter out \\r's in the
  134. input yourself, or explicitly use {fStrong}r{f}\/\\r\\n for "r$".
  135. #Indent
  136. {fEmphasis}<{fStrong}s{f}>{fStrong}r{f}{f}
  137. #Indent +4
  138. an {fStrong}r{f}, but only in start condition {fStrong}s{f} (see
  139. below for discussion of start conditions)
  140. <{fStrong}s1{f},{fStrong}s2{f},{fStrong}s3{f}>{fStrong}r{f}
  141. same, but in any of start conditions {fStrong}s1{f},
  142. {fStrong}s2{f}, or {fStrong}s3{f}
  143. #Indent
  144. {fEmphasis}<\*>{fStrong}r{f}{f}
  145. #Indent +4
  146. an {fStrong}r{f} in any start condition, even an exclusive one.
  147. #Indent
  148. {fEmphasis}<<EOF>>{f}
  149. #Indent +4
  150. an end-of-file
  151. <{fStrong}s1{f},{fStrong}s2{f}><<EOF>>
  152. an end-of-file when in start condition {fStrong}s1{f} or {fStrong}s2{f}
  153.  
  154. #Indent
  155.  
  156. Note that inside of a character class, all regular
  157. expression operators lose their special meaning except escape
  158. ('\\') and the character class operators, '-', ']', and, at
  159. the beginning of the class, '^'.
  160.  
  161. The regular expressions listed above are grouped according
  162. to precedence, from highest precedence at the top to
  163. lowest at the bottom.  Those grouped together have equal
  164. precedence.  For example,
  165.  
  166. #Wrap off
  167. #fCode
  168. foo|bar\*
  169. #f
  170. #Wrap on
  171.  
  172. is the same as
  173.  
  174. #Wrap off
  175. #fCode
  176. (foo)|(ba(r\*))
  177. #f
  178. #Wrap on
  179.  
  180. since the '\*' operator has higher precedence than
  181. concatenation, and concatenation higher than alternation ('|').
  182. This pattern therefore matches {fEmphasis}either{f} the string "foo" {fEmphasis}or{f}
  183. the string "ba" followed by zero-or-more r's.  To match
  184. "foo" or zero-or-more "bar"'s, use:
  185.  
  186. #Wrap off
  187. #fCode
  188. foo|(bar)\*
  189. #f
  190. #Wrap on
  191.  
  192. and to match zero-or-more "foo"'s-or-"bar"'s:
  193.  
  194. #Wrap off
  195. #fCode
  196. (foo|bar)\*
  197. #f
  198. #Wrap on
  199.  
  200. In addition to characters and ranges of characters,
  201. character classes can also contain character class
  202. {fUnderline}expressions{f}.  These are expressions enclosed inside {fEmphasis}[{f}: and {fEmphasis}:{f}]
  203. delimiters (which themselves must appear between the '['
  204. and ']' of the character class; other elements may occur
  205. inside the character class, too).  The valid expressions
  206. are:
  207.  
  208. #Wrap off
  209. #fCode
  210. [:alnum:] [:alpha:] [:blank:]
  211. [:cntrl:] [:digit:] [:graph:]
  212. [:lower:] [:print:] [:punct:]
  213. [:space:] [:upper:] [:xdigit:]
  214. #f
  215. #Wrap on
  216.  
  217. These expressions all designate a set of characters
  218. equivalent to the corresponding standard C {fEmphasis}isXXX{f} function.  For
  219. example, {fEmphasis}[:alnum:]{f} designates those characters for which
  220. {fEmphasis}isalnum(){f} returns true - i.e., any alphabetic or numeric.
  221. Some systems don't provide {fEmphasis}isblank(){f}, so flex defines
  222. {fEmphasis}[:blank:]{f} as a blank or a tab.
  223.  
  224. For example, the following character classes are all
  225. equivalent:
  226.  
  227. #Wrap off
  228. #fCode
  229. [[:alnum:]]
  230. [[:alpha:][:digit:]
  231. [[:alpha:]0-9]
  232. [a-zA-Z0-9]
  233. #f
  234. #Wrap on
  235.  
  236. If your scanner is case-insensitive (the {fEmphasis}-i{f} flag), then
  237. {fEmphasis}[:upper:]{f} and {fEmphasis}[:lower:]{f} are equivalent to {fEmphasis}[:alpha:]{f}.
  238.  
  239. Some notes on patterns:
  240.  
  241. #Indent +4
  242.  
  243.  - A negated character class such as the example
  244. "[^A-Z]" above {fEmphasis}will match a newline{f} unless "\\n" (or an
  245. equivalent escape sequence) is one of the
  246. characters explicitly present in the negated character
  247. class (e.g., "[^A-Z\\n]").  This is unlike how many
  248. other regular expression tools treat negated
  249. character classes, but unfortunately the inconsistency
  250. is historically entrenched.  Matching newlines
  251. means that a pattern like [^"]\* can match the
  252. entire input unless there's another quote in the
  253. input.
  254.  
  255.  
  256.  - A rule can have at most one instance of trailing
  257. context (the '\/' operator or the '$' operator).
  258. The start condition, '^', and "<<EOF>>" patterns
  259. can only occur at the beginning of a pattern, and,
  260. as well as with '\/' and '$', cannot be grouped
  261. inside parentheses.  A '^' which does not occur at
  262. the beginning of a rule or a '$' which does not
  263. occur at the end of a rule loses its special
  264. properties and is treated as a normal character.
  265.  
  266. The following are illegal:
  267.  
  268. #Wrap off
  269. #fCode
  270. foo\/bar$
  271. <sc1>foo<sc2>bar
  272. #f
  273. #Wrap on
  274.  
  275. Note that the first of these, can be written
  276. "foo\/bar\\n".
  277.  
  278. The following will result in '$' or '^' being
  279. treated as a normal character:
  280.  
  281. #Wrap off
  282. #fCode
  283. foo|(bar$)
  284. foo|^bar
  285. #f
  286. #Wrap on
  287.  
  288. If what's wanted is a "foo" or a
  289. bar-followed-by-a-newline, the following could be used (the special
  290. '|' action is explained below):
  291.  
  292. #Wrap off
  293. #fCode
  294. foo      |
  295. bar$     \/\* action goes here \*\/
  296. #f
  297. #Wrap on
  298.  
  299. A similar trick will work for matching a foo or a
  300. bar-at-the-beginning-of-a-line.
  301.  
  302. #Indent
  303.  
  304.